package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.R;
import com.gxa.entity.SysActionLog;
import com.gxa.mapper.LogMapper;
import com.gxa.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    /**
     * 分页查询所有系统日志
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R queryAllLogsByPage(int currentPage, int pageSize) {
        R r = new R();
        try {
            Page<SysActionLog> page = PageHelper.startPage(currentPage,pageSize);
            List<SysActionLog> sysActionLogList = logMapper.selectAllLogs();
            int count = (int) page.getTotal();
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setCount(count);
            r.setData(sysActionLogList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,查询失败,请联系管理员");
        }
        return r;
    }

    /**
     * 按条件分页查询系统日志
     * @param organization
     * @param operator
     * @param operationTime
     * @param operationContent
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R queryConditionalLog(String organization, String operator, Date operationTime, String operationContent, int currentPage, int pageSize) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("organization",organization);
        map.put("operator",operator);
        map.put("operationTime",operationTime);
        map.put("operationContent",operationContent);

        R r = new R();
        try {
            Page<SysActionLog> page = PageHelper.startPage(currentPage,pageSize);
            List<SysActionLog> sysActionLogList = logMapper.selectConditionalLog(map);
            int count = (int)page.getTotal();
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setCount(count);
            r.setData(sysActionLogList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,查询失败,请联系管理员");
        }
        return r;
    }
}
